package com.lanou.musicbox.fragment.localmusic;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.localmusic.CollectAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.custom.Events;
import com.lanou.musicbox.entity.Entity;
import com.lanou.musicbox.entity.EntityDao;
import com.lanou.musicbox.utils.MyConfig;
import com.lanou.musicbox.utils.fragment_back.DaoSingleton;

import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;
import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 15/11/9.
 */
public class CollectFragment extends BaseFragment implements AdapterView.OnItemLongClickListener, View.OnClickListener, AdapterView.OnItemClickListener {
    private ListView listView;
    private CollectAdapter adapter;
    private List<Entity> data;
    private DaoSingleton daoSingleton;
    private EntityDao entityDao;
    private QueryBuilder queryBuilder;
    private Entity entity;
    private ImageButton collect_comback;
    private CollectMusic listener;
  private LinearLayout collect_linear;
    public interface CollectMusic {
        void itemMusicId(int position);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        listener = (CollectMusic) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.collect_list, null);
        listView = (ListView) view.findViewById(R.id.collect_list);
        collect_comback = (ImageButton) view.findViewById(R.id.collect_comback);
        collect_linear= (LinearLayout) view.findViewById(R.id.collect_linear);
        entity = new Entity();
        daoSingleton = DaoSingleton.getInstance(getActivity());//初始化单例
        entityDao = daoSingleton.getEntityDao();//标初始化
        queryBuilder = entityDao.queryBuilder();
        data = entityDao.queryBuilder().list();//查询数据库
        adapter = new CollectAdapter(getActivity(), data);
        listView.setAdapter(adapter);
        Log.i("有值吗", "_--------- " + data);
        collect_comback.setOnClickListener(this);
        listView.setOnItemLongClickListener(this);
        listView.setOnItemClickListener(this);
        return view;
    }

    //获得背景图片
    @Override
    public void onResume() {
        super.onResume();
        MyConfig setting = new MyConfig(getActivity(), false);
        collect_linear.setBackgroundResource(setting.getCurrentSkinResID());
    }


    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        entity = (Entity) parent.getItemAtPosition(position);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("是否删除");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                if (entity != null) {

                    entityDao.queryBuilder().where(EntityDao.Properties.Title.eq(entity.getTitle())).buildDelete().executeDeleteWithoutDetachingEntities();
                    adapter.remove(entity);
                    Log.i("pppppp", " " + entity);
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        builder.show();
        return false;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.collect_comback:
                getFragmentManager().popBackStack();
                break;
        }
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Events.MusicInfoEvent musicInfoEvent = new Events.MusicInfoEvent();
        String whatMusic = "本地";
        musicInfoEvent.setMp3Infos(data, whatMusic);
        EventBus.getDefault().post(musicInfoEvent);
        listener.itemMusicId(position);

    }
}

